﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Takkou
{
	public partial class Mission1 : UserControl
	{
        public const int TERRAIN_WIDTH = 20;
        public const int TERRAIN_HEIGHT = 15;
        public const int GRID_SIZE = 50;


        public Mission1()
		{
			// Required to initialize variables
			InitializeComponent();
            Loaded += new RoutedEventHandler(Mission1_Loaded);
		}

        void Mission1_Loaded(object sender, RoutedEventArgs e)
        {
            CreateGrid();
            CompositionTarget.Rendering +=new EventHandler(update);
            Core.Game.setupScreen(this);
        }

        private void update(object sender, System.EventArgs e)
        {
            RenderingEventArgs r = e as RenderingEventArgs;
            Core.Game.Update(r.RenderingTime.Ticks);
        }
            
        private void CreateGrid()
        {
            Border[,] cell = new Border[TERRAIN_WIDTH, TERRAIN_HEIGHT];

            Color lightGray = Color.FromArgb(255, 200, 200, 200);
            SolidColorBrush gridBrush = new SolidColorBrush(lightGray);
            Thickness thick = new Thickness(1);

            for (int i = 0; i < TERRAIN_WIDTH; i++)
                for (int j = 0; j < TERRAIN_HEIGHT; j++)
                {
                    cell[i, j] = new Border()
                    {
                        Width = 51,
                        Height = 51,
                        BorderBrush = gridBrush,
                        BorderThickness = thick,
                    };
                    cell[i, j].SetValue(Canvas.LeftProperty, (double)i * 50);
                    cell[i, j].SetValue(Canvas.TopProperty, (double)j * 50);
                    WorldArea.Children.Add(cell[i, j]);
                }
        }

        private void MissionFinished(object sender, System.Windows.RoutedEventArgs e)
		{
			PageSwitcher ps = this.Parent as PageSwitcher;
			ps.Navigate( new MainPage() );
		}
	}
}